/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 * 
 */
package seedpod.webapp.view.seedpodwidgets;

import seedpod.exceptions.InvalidWidgetException;
import seedpod.model.AVPair;
import seedpod.model.ProtegeValueType;
import seedpod.model.SeedpodModel.RdbCls.FormWidget;
import seedpod.model.SeedpodModel.RdbCls.ViewWidget;
import seedpod.model.rdb.RdbValueType;
import seedpod.webapp.view.htmlwidget.TextInput;

public class Text extends LimsWidget {

	public Text() {
	// TODO Auto-generated constructor stub
	}

	public Text(AVPair avpair) {
		super( avpair );
	}

	@Override
	public String doEdit() {
		return render();
	}

	@Override
	public String doView() {
		return super.doView();
	}

	@Override
	public String getData() {
		Object data = _avPair.getValue();
		return data == null ? "" : data.toString();
	}

	@Override
	public FormWidget getFormWidgetType() {
		return FormWidget.TEXT;
	}

	protected int getInputSize() {
		Integer paramSize;
		String paramSizeStr;

		paramSizeStr = getAttribute().getFormWidgetParam();
		if ( paramSizeStr != null && paramSizeStr.length() > 0 )
			try {
				paramSize = Integer.decode( paramSizeStr );
			} catch (NumberFormatException nfe) {
				paramSize = 20;
			}
		else paramSize = getValueLength(); // new Integer( TextInput.DEFAULT_LENGTH );

		if ( paramSize.intValue() > TextInput.MAX_LENGTH ) paramSize = new Integer( TextInput.MAX_LENGTH );

		return paramSize;
	}

	private Integer getValueLength() {
		Integer valueLength = getAttribute().getRdbValueLength();
		if ( valueLength == null ) valueLength = new Integer( TextInput.MAX_LENGTH );
		return valueLength;
	}

	@Override
	public ViewWidget getViewWidgetType() {
		return null;
	}

	/**
	 * actually try to render anything in Text if possible
	 */
	@Override
	public String render() {
		TextInput txInput = new TextInput( _avPair.genRefId(), getData() );
		if ( !getAttribute().isAssociated() ) txInput.disableInput( true );
		txInput.setCssClass( "text" );
		txInput.setMaxLength( TextInput.MAX_LENGTH );
		txInput.setSize( getInputSize() );
		return txInput.render();
	}

	@Override
	public void setData( Object str ) {
		super.setData( _avPair.getValue() );
	}

	@Override
	public void validateWidget() throws InvalidWidgetException {
		boolean valid = true;

		RdbValueType dbType = getAttribute().getRdbValueType();
		ProtegeValueType prType = getAttribute().getProtegeValueType();

		// database type must be varchar, integer, numeric, & text
		if ( !dbType.equals( RdbValueType.VARCHAR ) && !dbType.equals( RdbValueType.INTEGER ) && !dbType.equals( RdbValueType.NUMERIC ) && !dbType.equals( RdbValueType.TEXT ) )
			valid = false;

		// protege type must be a float, integer, or string
		if ( !prType.equals( ProtegeValueType.Float ) && !prType.equals( ProtegeValueType.Integer ) && !prType.equals( ProtegeValueType.String ) )
			valid = false;

		if ( !valid )
			throw new InvalidWidgetException( " The data value type must be a string or numeric to use this Text widget. " );
	}

}